package com.lb.stream;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class StreamFilterTest {
    public static List<Dog> dogs = null;
    static {
            dogs = new ArrayList<Dog>(){
            {
                add(new Dog("黄一",11, new Date()));
                add(new Dog("黄一",22, new Date()));
                add(new Dog("黄三",33, new Date()));
            }
        };

    }
    @SuppressWarnings("AlibabaAvoidManuallyCreateThread")
    public static void main(String[] args) {

        List<Dog> list =dogs.stream()
                .sorted(Comparator.comparing(Dog::getTime).reversed())
                .filter(distinctByKey(Dog::getName)).collect(Collectors.toList());
        list.forEach(System.out::println);

        Map<String, Object> map = new HashMap<>();
        map.put("type","abc");
        map.put("type","cde");
        System.out.println(map);
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }

    static class Dog {
        private String name;
        private int age;
        private Date time;

        public Dog(String name, int age, Date time) {
            this.name = name;
            this.age = age;
            this.time = time;

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Date getTime() {
            return time;
        }

        public void setTime(Date time) {
            this.time = time;
        }

        @Override
        public String toString() {
            return "Dog{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}